<?php
/**
 * This is WFWidgetPoolOpt class
 * Manipulate all the option associate array of WidgetPool
 * @package inc/class/widgetPool/config
 * @author William Su
 */
class WFWidgetPoolOpt
{
    /**
     * @var WF
     */
    private $objFramework;
    const WP_UPDATE_STATUS_NO_NEED_UPDATE = '0';
    const WP_UPDATE_STATUS_NEED_ENABLE = '1';
    const WP_UPDATE_STATUS_NEED_UPDATE = '2';
    const WP_UPDATE_RESULT_NOT_YET_UPDATE = '0';
    const WP_UPDATE_RESULT_UPDATE_SUCCESS = '1';
    const WP_UPDATE_RESULT_UPDATE_FAILED = '2';
    
    const WIDGETPOOL_UPDATELOG_STATUS_ONGOING = '0';
    const WIDGETPOOL_UPDATELOG_STATUS_SUCCESS = '1';
    const WIDGETPOOL_UPDATELOG_STATUS_FAILED = '2';

    public function __construct($objFramework){
        $this->objFramework = $objFramework;
    }
    private static $instance;
    /**
     *\brief Description
     * - Option of Table WIDGET.widget_status
     * @return array
     */
    public function getWidgetStatus(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret['0'] = 'disabled'; //widget is unuse 
            $ret['1'] = 'enabled'; //widget is inuse
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Option of Table WIDGET.widget_status_update
     * @return array
     */
    public function getWidgetStatusUpdate(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret['0'] = 'original widget'; //widget is default 
            $ret['1'] = 'new widget from Widget Pool'; //widget is new 
            $ret['2'] = 'upgrade widget from Widget Pool'; //widget is upgrade
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Option of widget_type which show in AddWidgets UI
     * @return array
     */
    public function getWidgetTypeInAddWidgets(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret['1'] = 'widget';
            $ret['2'] = 'widget template';
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Option of Table WIDGET.widget_type
     * @return array
     */
    public function getWidgetType(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = $this->objFramework->getWidgetPoolFactory()->getWidgetPoolOpt()->getWidgetTypeInAddWidgets();
            $ret['0'] = 'modname';
            //$ret['1'] = 'widget';
            //$ret['2'] = 'widget template';
            $ret['3'] = 'configure widget';
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Get Option Array of WidgetPool relative TableName 
     * - For interface initWidgetPoolDBSchema.php used
     * @return array
     */
    public function getWidgetPoolTableName(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[] = 'WIDGET';
            $ret[] = 'WIDGET_CATEGORY';
            //$ret[] = 'wf_version';
            $ret[] = 'WIDGET_CATEGORY_MAPPING';
            $ret[] = 'WP_UPDATE_LOG';
            $ret[] = 'WP_UPDATE_FILTER';
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Get Option Array of TableWidget relative column 
     * - For validate widget.xml used
     * @return array
     */
    public function getTableSchemaColumnWidget(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[] = 'widget_id'; //moduleSeqID
            $ret[] = 'widget_name';
            $ret[] = 'modname';
            $ret[] = 'widget_version';
            $ret[] = 'widget_version_float';
            $ret[] = 'widget_min_wf_version'; //minSupportWidgetFrameworkVersion
            $ret[] = 'widget_min_wf_version_float'; //minSupportWidgetFrameworkVersion
            $ret[] = 'widget_status'; //0=off,1=on
            $ret[] = 'widget_status_update'; //0=default,1=new,2=version upgrade
            $ret[] = 'widget_type'; //0=modname,1=widget,2=widgetTemplate
            $ret[] = 'user_generated'; //0=no,1=yes
            $ret[] = 'icon';
            $ret[] = 'icon_preview';
            $ret[] = 'widget_key';
            $ret[] = 'param';
            $ret[] = 'createtime';
            $ret[] = 'edittime'; //lastupdate (initial is equal to createtime)
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Get Option Array of TableWidgetCategory relative column
     * - For validate widget.xml used
     * @return array
     */
    public function getTableSchemaColumnWidgetCategory(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[] = 'category_id';
            $ret[] = 'category_name';
            $ret[] = 'createtime';
            $ret[] = 'edittime';
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Get Option Array of TableWidgetCategoryMapping relative column
     * - For validate widget.xml used
     * @return array
     */
    public function getTableSchemaColumnWidgetCategoryMapping(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[] = 'mapping_id';
            $ret[] = 'widget_id';
            $ret[] = 'category_id';
            $ret[] = 'min_support_version';
            $ret[] = 'min_support_version_float';
            $ret[] = 'createtime';
            $ret[] = 'edittime';
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Get Option Array of TableWidgetComponent relative column 
     * - For validate component.xml used
     * @return array
     */
    public function getTableSchemaColumnWidgetComponent(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[] = 'wcomponent_id'; //moduleSeqID
            $ret[] = 'wcomponent_name';
            $ret[] = 'wcomponent_version';
            $ret[] = 'wcomponent_version_float';
            $ret[] = 'wcomponent_min_support_version'; //minSupportOSCEVersion
            $ret[] = 'wcomponent_min_support_version_float'; //minSupportOSCEVersion
            $ret[] = 'wcomponent_status'; //0=off,1=on
            $ret[] = 'wcomponent_status_update'; //0=default,1=new,2=version upgrade
            $ret[] = 'wcomponent_param';
            $ret[] = 'createtime';
            $ret[] = 'edittime'; //lastupdate (initial is equal to createtime)
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Get Option Array of TableWidgetComponentCategory relative column
     * - For validate component.xml used
     * @return array
     */
    public function getTableSchemaColumnWidgetComponentCategory(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[] = 'category_id';
            $ret[] = 'category_name';
            $ret[] = 'category_product_id';
            $ret[] = 'category_proxy';
            $ret[] = 'createtime';
            $ret[] = 'edittime';
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    /**
     *\brief Description
     * - Get Option Array of TableWidgetComponentCategoryMapping relative column
     * - For validate component.xml used
     * @return array
     */
    public function getTableSchemaColumnWidgetComponentCategoryMapping(){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[] = 'mapping_id';
            $ret[] = 'wcomponent_id';
            $ret[] = 'category_id';
            $ret[] = 'min_support_version';
            $ret[] = 'min_support_version_float';
            $ret[] = 'createtime';
            $ret[] = 'edittime';
            self::$instance[__FUNCTION__] = $ret;
        }
        return self::$instance[__FUNCTION__];
    }
    const WIDGETPOOL_UPDATE_FAILED_IS_NOT_MUTEX = '90';
    const WIDGETPOOL_UPDATE_FAILED_GET_LOGID = '100';
    const WIDGETPOOL_UPDATE_FAILED_MKDIR_WPLOGID = '200';
    const WIDGETPOOL_UPDATE_FAILED_REMOVE_WIDGETS_NEW_OLD = '225';
    const WIDGETPOOL_UPDATE_FAILED_UPDATE_BEFORE_UPDATE_WIDGETS = '230';
    const WIDGETPOOL_UPDATE_FAILED_UPDATE_BEFORE_UPDATE_WIDGET_COMPONENTS = '235';
    const WIDGETPOOL_UPDATE_FAILED_COPY_WIDGETS_NEW_TO_WPLOGID = '250';
    const WIDGETPOOL_UPDATE_FAILED_COPY_CURRENT_WIDGET_AND_WIDGET_COMPONENT_TO_WPLOGID = '300';
    const WIDGETPOOL_UPDATE_FAILED_ADDORUPGRADE_WIDGET_AND_WIDGET_COMPONENT_TO_WPLOGID = '325';
    const WIDGETPOOL_UPDATE_FAILED_ADD_NEW_WIDGET_TO_WPLOGID = '350';
    const WIDGETPOOL_UPDATE_FAILED_UPGRADE_WIDGET_TO_WPLOGID = '400';
    const WIDGETPOOL_UPDATE_FAILED_CHANGE_WP_PATH_TO_WPLOGID = '450';
    const WIDGETPOOL_UPDATE_FAILED_UPDATE_SUCCESS_STATUS = '500';
    const WIDGETPOOL_UPDATE_FAILED_EXEC_DB_UPDATE_FILTER = '550';
    const WIDGETPOOL_UPDATE_FAILED_RM_WIDGETS_NEW = '600';
    const WIDGETPOOL_UPDATE_FAILED_COPY_PATH_TOO_LONG = '650';
    const WIDGETPOOL_UPDATE_FAILED_WIDGETS_NEW_NOEXISTED = '660';
    const WIDGETPOOL_UPDATE_FAILED_WIDGETS_NEW_NOREAD_PERMISSIONS = '665';
    const WIDGETPOOL_UPDATE_FAILED_WIDGETS_NEW_NOWRITE_PERMISSIONS = '670';
    const WIDGETPOOL_UPDATE_FAILED_WIDGET_POOL_NOREAD_PERMISSIONS = '675';
    const WIDGETPOOL_UPDATE_FAILED_WIDGET_POOL_NOWRITE_PERMISSIONS = '680';
    const WIDGETPOOL_UPDATE_FAILED_EXEC_INTERFACE_SYN_SERVERLISTTYPE = '690';
    public function getUpdateLogFailPoint($int = null){
        if(! isset(self::$instance[__FUNCTION__][$int])){
            $ret = array();
            $ret[self::WIDGETPOOL_UPDATE_FAILED_IS_NOT_MUTEX] = 'Another Update widget process is running! There is not allowed concurrent Update  widget proccess!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_GET_LOG_ID] = 'Failed to get LogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_MKDIR_WPLOGID] = 'Failed to mkdir folder /path/to/widgetPool/wpLogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_REMOVE_WIDGETS_NEW_OLD] = 'Failed to remove folder /path/to/repository/widgets_new_old!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_UPDATE_BEFORE_UPDATE_WIDGETS] = 'Failed to update WP_UPDATE_LOG.before_update_widgets!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_COPY_WIDGETS_NEW_TO_WPLOGID] = 'Failed to copy /path/to/widgets_new/* excerpt /path/to/widgets_new/widget to /path/to/widgetPool/wpLogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_COPY_CURRENT_WIDGET_TO_WPLOGID] = 'Failed to copy /path/to/wpCurrent/widget to /path/to/widgetPool/wpLogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_ADDORUPGRADE_WIDGET_TO_WPLOGID] = 'Failed to Add or Upgrade widget to /path/to/widgetPool/wpLogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_ADD_NEW_WIDGET_TO_WPLOGID] = 'Failed to Add new widget to /path/to/widgetPool/wpLogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_UPGRADE_WIDGET_TO_WPLOGID] = 'Failed to Upgrade widget to /path/to/widgetPool/wpLogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_CHANGE_WP_PATH_TO_WPLOGID] = 'Failed to Change CurrentWP Path to /path/to/widgetPool/wpLogId!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_UPDATE_SUCCESS_STATUS] = 'Failed to Update WP_UPDATE_LOG.status=success!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_EXEC_DB_UPDATE_FILTER] = 'Failed to execute all filter rules in WP_UPDATE_FILTER!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_RM_WIDGETS_NEW] = 'Failed to rm folder /path/to/widgets_new';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_COPY_PATH_TOO_LONG] = 'Failed to copy, copy path /path/to/wpLogId/path/to/longest/file too long!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_WIDGETS_NEW_NOEXISTED] = 'Failed to update, /path/to/widgets_new not existed!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_WIDGETS_NEW_NOREAD_PERMISSIONS] = 'Failed to update, /path/to/widgets_new* no read permissions!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_WIDGETS_NEW_NOWRITE_PERMISSIONS] = 'Failed to update, /path/to/widgets_new* no write permissions!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_WIDGET_POOL_NOREAD_PERMISSIONS] = 'Failed to update, /path/to/widgetPool no read permissions!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_WIDGET_POOL_NOWRITE_PERMISSIONS] = 'Failed to update, /path/to/widgetPool no write permissions!';
            $ret[self::WIDGETPOOL_UPDATE_FAILED_EXEC_INTERFACE_SYN_SERVERLISTTYPE] = 'Failed to execute php /path/to/interface_request.php synServerListType.php update!';
            if(null !== $int){
                $ret = $ret[$int];
            }
            self::$instance[__FUNCTION__][$int] = $ret;
        }
        return self::$instance[__FUNCTION__][$int];
    }
    public function getUpdateLogStatus($int = null){
        if(! isset(self::$instance[__FUNCTION__][$int])){
            $ret = array();
            $ret[self::WIDGETPOOL_UPDATELOG_STATUS_ONGOING] = 'Widget is updating from WidgetPool...';
            $ret[self::WIDGETPOOL_UPDATELOG_STATUS_SUCCESS] = 'Widget updated success from WidgetPool';
            $ret[self::WIDGETPOOL_UPDATELOG_STATUS_FAILED] = 'Widget updated failed from WidgetPool';
            if(null !== $int){
                $ret = $ret[$int];
            }
            self::$instance[__FUNCTION__][$int] = $ret;
        }
        return self::$instance[__FUNCTION__][$int];
    }
    /**
     *\brief Description
     * - Get Option array of widget/configure widget/widget component update_status for UI
     * @return array
     */
    public function getUpdateStatusForUI($intUpdateStatus = null){
        if(! isset(self::$instance[__FUNCTION__])){
            $ret = array();
            $ret[self::WP_UPDATE_STATUS_NO_NEED_UPDATE] = 'ok';
            $ret[self::WP_UPDATE_STATUS_NEED_ENABLE] = 'enable';
            $ret[self::WP_UPDATE_STATUS_NEED_UPDATE] = 'update';
            self::$instance[__FUNCTION__] = $ret;
        }
        if(null !== $intUpdateStatus){
            //var_dump(self::$instance[__FUNCTION__][$intUpdateStatus]);exit();
            return isset(self::$instance[__FUNCTION__][$intUpdateStatus]) ? self::$instance[__FUNCTION__][$intUpdateStatus] : '';
        }
        return self::$instance[__FUNCTION__];
    }
}